import axios from 'axios';

// 获取产品列表
export const getServiceList = async (params: any) => {
  try {
    const response = await axios.post('/serviceProduct/getList', params);
    return  { success: true,data: response.data.list,total: response.data.total }
  } catch (error) {
    console.error('获取产品列表失败', error);
    return { success: false, message: '获取产品列表失败' };
  }
};

// 获取产品详情
export const getServiceDetail = async (id: number) => {
  try {
    const response = await axios.get(`/serviceProduct/getDetail?id=${id}`);
    return response;
  } catch (error) {
    console.error('获取产品详情失败', error);
    return { success: false, message: '获取产品详情失败' };
  }
};

// 保存/修改产品
export const saveServiceProduct = async (data: any) => {
  try {
    const response = await axios.post('/serviceProduct/saveServiceProduct', data);
    return response;
  } catch (error) {
    console.error('保存/修改产品失败', error);
    return { success: false, message: '保存/修改产品失败' };
  }
};

// 删除产品
export const deleteServiceProduct = async (id: number) => {
  try {
    const response = await axios.post('/serviceProduct/delete', { id });
    return response;
  } catch (error) {
    console.error('删除产品失败', error);
    return { success: false, message: '删除产品失败' };
  }
};

// 保存规格组和规格
export const saveSpec = async (data: any) => {
  try {
    const response = await axios.post('/spec/saveSpec', data);
    return response.data;
  } catch (error) {
    console.error('保存规格组和规格失败', error);
    return { success: false, message: '保存规格组和规格失败' };
  }
};

// 删除规格
export const deleteSpecValue = async (id: number) => {
  try {
    const response = await axios.post('/spec/deleteSpecValue', { id });
    return response.data;
  } catch (error) {
    console.error('删除规格失败', error);
    return { success: false, message: '删除规格失败' };
  }
};

// 删除规格组
export const deleteSpec = async (id: number) => {
  try {
    const response = await axios.post('/spec/deleteSpec', { id });
    return response.data;
  } catch (error) {
    console.error('删除规格组失败', error);
    return { success: false, message: '删除规格组失败' };
  }
};

// 添加规格
export const saveSpecValue = async (data: any) => {
  try {
    const response = await axios.post('/spec/saveSpecValue', data);
    return response.data;
  } catch (error) {
    console.error('添加规格失败', error);
    return { success: false, message: '添加规格失败' };
  }
};

// 查询规格树

export const getSpecTreeList = async (params: any) => {
  try {
    console.log(params,'params')
    const response = await axios.get('/serviceProduct/getSpecTreeList?productId='+params );
    return response;
  } catch (error) {
    console.error('查询规格树失败', error);
    return { success: false, message: '查询规格树失败' };
  }
};

// 获取产品列表-不分页
export const getServiceListNoPage = async (params: any) => {
  
  try {
    if(params){
      const response = await axios.get('/serviceProduct/getSelectList?type='+params);
      return response;
    }

   
    
  } catch (error) {
    console.error('获取产品列表失败', error);
    return { success: false, message: '获取产品列表失败' };
  }
};

// 获取用户信息
export const getProjectList = async (params) => {
  try {
    const response = await axios.get('/demand/getProjectList',{params:params});
    return {
      data: response.data,
      success: true
      
    } ;
  } catch (error) {
    console.error('获取用户信息失败', error);
    return { success: false, message: '获取用户信息失败' };
  }
};

// 计算价格
export const getPrice = async (params: any) => {
  try {
    const response = await axios.get('/serviceProduct/getSpecDetail', {params:params});
    return {
      data: response.data,
      success: true
    };
  } catch (error) {
    console.error('获取价格信息失败', error);
    return { success: false, message: '获取价格信息失败' };
  }
};

// 获取退改规格详情
export const getRefundSpecDetail = async (params: any) => {
  try {
    const response = await axios.get('/refundRule/getDetail', {params:params});
    return {
      data: response.data,
      success: true
    };
  } catch (error) {
    console.error('获取退改规格详情失败', error);
    return { success: false, message: '获取退改规格详情失败' };
  }
}

// 修改退改规则
export const saveRefundSpec = async (data: any) => {
  try {
    const response = await axios.post('/refundRule/saveRule', data);
    return {
      data: response.data,
      success: true
    };
  } catch (error) {
    console.error('修改退改规则失败', error);
    return { success: false, message: '修改退改规则失败' };
 }
}